import { NativeHelper } from "../../native/NativeHelper";
import { argType } from "../IPublisher";
import { IShareAble } from "../IShareAble";


export class NativeShare extends IShareAble{
    protected _className:string = "sdk.{0}.Share";
    init(type: string): IShareAble {
        if(!this._type){
            this._type = type;
            this._className = this._className.replace("{0}", this._type);
        }
        return this;
    }
    shareText(data: string | argType[], caller?: any, onSuccess?: Function, onFail?: Function): void {
        this._share("shareText", data, caller, onSuccess, onFail);
    }
    shareImage(data: string | argType[], caller?: any, onSuccess?: Function, onFail?: Function): void {
        this._share("shareImage", data, caller, onSuccess, onFail);
    }
    shareVideo(data: string | argType[], caller?: any, onSuccess?: Function, onFail?: Function): void {
        this._share("shareVideo", data, caller, onSuccess, onFail);
    }

    protected _share(action:string, data:string | argType[], caller?: any, onSuccess?: Function, onFail?: Function){
        let callbacks:string[] = [];
        callbacks.push(onSuccess != null ? NativeHelper.registerCallbackOnce(caller, onSuccess) : "");
        callbacks.push(onFail != null ? NativeHelper.registerCallbackOnce(caller, onFail) : "");
        if(typeof data == "string"){
            NativeHelper.call(this._className, action, data, ...callbacks);
        }else{
            NativeHelper.call(this._className, action, ...data, ...callbacks);
        }
    }

}
